// 0pts

#include <algorithm>
#include <cstdio>
#include <cstdlib>
constexpr int MaxN = 1e2 + 5;
int n;
long long a[MaxN];
long long b[MaxN];
long long c[MaxN];
inline void solve()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld", &a[i]);
    }
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld", &b[i]);
    }
    for (int i = 1; i <= n; i++)
    {
        if (a[i] == 0 && b[i] != 0)
        {
            printf("-1\n");
            return;
        }
        c[i] = b[i] - a[i];
    }
    long long answer = 0;
    for (int len = n; len >= 1; len--)
    {
        for (int l = 1; l + len - 1 <= n; l++)
        {
            int r = l + len - 1;
            long long w = c[l];
            for (int k = l + 1; k <= r; k++)
            {
                if (a[k] == 0 || (c[k] == 0 && b[k] == 0))
                {
                    continue;
                }
                if ((w > 0) ^ (c[k] > 0))
                {
                    w = 0;
                    break;
                }
                else
                {
                    if (w > 0)
                    {
                        w = std::min(w, c[k]);
                    }
                    else
                    {
                        w = std::max(w, c[k]);
                    }
                }
            }
            if (w == 0)
            {
                continue;
            }
            w = -w;
            answer += std::abs(w);
            for (int k = l; k <= r; k++)
            {
                if (a[k] == 0 || (c[k] == 0 && b[k] == 0))
                {
                    continue;
                }
                c[k] += w;
            }
        }
    }
    printf("%lld\n", answer);
}
int main()
{
    freopen("game.in", "r", stdin);
    freopen("game.out", "w", stdout);
    int t;
    scanf("%d", &t);
    for (int i = 1; i <= t; i++)
    {
        solve();
    }
    return 0;
}
